package com.mobileares.midp.demo.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.*;
import com.mobileares.midp.widgets.client.anchor.Split;
import com.mobileares.midp.widgets.client.anchor.TextAnchor;
import com.mobileares.midp.widgets.client.frame.Frame;
import com.mobileares.midp.widgets.client.frame.Header;
import com.mobileares.midp.widgets.client.frame.MainPanel;
import com.mobileares.midp.widgets.client.panel.DecoratePanel;
import com.mobileares.midp.widgets.client.panel.MaxPanel;
import com.mobileares.midp.widgets.client.tab.FrameTabPanel;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-7-21
 * Time: 8:18:20
 * To change this template use File | Settings | File Templates.
 */
public class DemoDashboard implements EntryPoint {
    static Frame frame;
    public static FrameTabPanel tabPanel;

    public void onModuleLoad() {
        frame = new Frame();
        RootPanel.get("frame").add(frame);
        initHead("");
        initMenu();
        initMain();
        initDefaultTab();
        DeferredCommand.addCommand(new Command() {
            public void execute() {
                frame.reSize();
            }
        });

    }

    private void initDefaultTab() {
        DecoratePanel panel = new DecoratePanel();
//        panel.setHeight(DemoDashboard.frame.getMainHeight() - 40);
        panel.addWidget(new Image("fishimages/darling.jpg"));
        tabPanel.addItem("欢迎使用", panel);
        tabPanel.getItem("欢迎使用").setClose(false);
        tabPanel.setSelectItem("欢迎使用");
    }

    private void initMenu() {
        MainPanel main = frame.getMain();
//        DecoratePanel publishBoard = new DecoratePanel();
//        publishBoard.getContainer().setWidget(getWeather());
//        publishBoard.setHeight(150);
//        main.addMenuWidget(publishBoard);
        main.addMenuWidget(MenuBuilder2.getMenuWidget());
    }

//    private Widget getWeather() {
//        HTML html = new HTML();
//        html.setHTML("<iframe name=\"weather_inc\" src=\"http://tianqi.xixik.com/cframe/4\" width=\"150\" height=\"120\"" +
//                " frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" ></iframe>");
//        return html;
//    }

    private void initMain() {
        final MainPanel main = frame.getMain();
        tabPanel = new FrameTabPanel();
        main.setMainWidget(tabPanel);
//        Window.addResizeHandler(new ResizeHandler() {
//            public void onResize(ResizeEvent resizeEvent) {
//                int height = Window.getClientHeight();
//                main.setMainHeight(height - 50 );
//            }
//        });
    }

    private void initHead(String text) {
        Header header = frame.getHeader();
        header.setWelcomeText(text);
        FlowPanel fp = new FlowPanel();
        TextAnchor exit = new TextAnchor("退出");
        exit.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                //todo
            }
        });
        TextAnchor reLogin = new TextAnchor("重登录");
        reLogin.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                //todo
            }
        });
        TextAnchor max = new TextAnchor("最大化");
        max.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                Widget widget = tabPanel.getSelectItem().getShowWidget();
                MaxPanel mp = new MaxPanel();
                mp.setMaxWidget(widget);
                mp.addResumeListener(new MaxPanel.IResume() {
                    public void process(Widget widget) {
                        tabPanel.getSelectItem().setShowWidget(widget);
                        tabPanel.setSelectItem(tabPanel.getSelectItem().getUserObject());
                    }
                });
                mp.show();
            }
        });
        fp.add(max);
        fp.add(new Split());
        fp.add(reLogin);
        fp.add(new Split());
        fp.add(exit);
        header.setFunctionBarWidget(fp);
    }
}
